package hz.mobileproject.pda.toolsmanager.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.xutils.view.annotation.ViewInject;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.toolsmanager.bean.ToolData;
import hz.mobileproject.pda.toolsmanager.bean.response.RP_LoadCallList;
import hz.mobileproject.pda.ui.base.BaseCustomExpandableLvAdapter;

/**
 * Created by Lc on 2018/4/27.
 */

public class ToolsCallAdapter extends BaseCustomExpandableLvAdapter<RP_LoadCallList,ToolData,
        ToolsCallAdapter.ParentVH,
        ToolsCallAdapter.ChildrenVH> {

    private int mSelectPosition = -1;

    public ToolsCallAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    protected int bindParentView() {
        return R.layout.item_tools_call_parent;
    }

    @Override
    protected int bindChildrenView() {
        return R.layout.item_tools_call_children;
    }

    @Override
    protected ParentVH createParentViewHolder() {
        return new ParentVH();
    }

    @Override
    protected ChildrenVH createChildrenViewHolder() {
        return new ChildrenVH();
    }

    @Override
    protected void bindParentData(int position, View convertView, ViewGroup viewGroup, RP_LoadCallList station, ParentVH viewHolder) {
        //显示有问题   另一种方法实现
//        if(viewGroup != null){
//            //默认展开
//            ExpandableListView expandableListView = (ExpandableListView) viewGroup;
//            if(!expandableListView.isGroupExpanded(position)){
//                expandableListView.expandGroup(position);
//            }
//        }
        viewHolder.tv_group_name.setText(station.getGroup_name());
        viewHolder.tv_tools_place_name.setText(station.getStation_name());
        viewHolder.tv_tools_person_name.setText(station.getUser_name());
        if(position == mSelectPosition){
            viewHolder.ll_tools_call_parent_bg.setBackgroundResource(R.drawable.bg_tools_call_lv_parent_item_selected);
        }else{
            viewHolder.ll_tools_call_parent_bg.setBackgroundResource(R.drawable.bg_tools_call_lv_parent_item);
        }
    }

    @Override
    protected void bindChildrenData(int groupPosition, int childrenPosition, View convertView, ViewGroup viewGroup, ToolData tool, ChildrenVH viewHolder) {
        viewHolder.tv_tool_name.setText(tool.getTool_name());
        viewHolder.tv_tools_tool_id.setText(tool.getTool_id());
        if(childrenPosition == mParentList.get(groupPosition).getChildrenList().size()-1){
            viewHolder.ll_tools_call_children_bg.setBackgroundResource(R.drawable.bg_tools_call_lv_children_item);
        }else{
            viewHolder.ll_tools_call_children_bg.setBackgroundColor(Color.WHITE);
        }
    }

    public int getmSelectPosition() {
        return mSelectPosition;
    }

    public void setmSelectPosition(int mSelectPosition) {
        this.mSelectPosition = mSelectPosition;
    }

    public void setSelectGroup(ExpandableListView expandableListView, int position){
        mSelectPosition = position;
        this.notifyDataSetInvalidated();
        expandableListView.setSelectedGroup(position);
    }

    class ParentVH {
        @ViewInject(R.id.tv_tools_group_name)
        private TextView tv_group_name;
        @ViewInject(R.id.tv_tools_place_name)
        private TextView tv_tools_place_name;
        @ViewInject(R.id.tv_tools_person_name)
        private TextView tv_tools_person_name;
        @ViewInject(R.id.ll_tools_call_parent_bg)
        private LinearLayout ll_tools_call_parent_bg;

    }

    class ChildrenVH {
        @ViewInject(R.id.tv_tools_tool_name)
        private TextView tv_tool_name;
        @ViewInject(R.id.tv_tools_tool_id)
        private TextView tv_tools_tool_id;
        @ViewInject(R.id.ll_tools_call_children_bg)
        private LinearLayout ll_tools_call_children_bg;
    }
}
